package org.example.config.security;

import com.fasterxml.jackson.databind.ObjectMapper;
import org.example.controller.HttpResult;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.AuthenticationEntryPoint;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

public class CustomAuthenticationEntryPoint implements AuthenticationEntryPoint {
    ObjectMapper objectMapper=new ObjectMapper();
    @Override
    public void commence(HttpServletRequest request, HttpServletResponse response, AuthenticationException authException) throws IOException, ServletException {
        HttpResult<Void> httpResult=HttpResult.error("未登录");
        String json=objectMapper.writeValueAsString(httpResult);
        response.setCharacterEncoding("utf8");
        response.setContentType("application/json");
        response.getWriter().write(json);
        response.getWriter().close();
    }
}
